var t = require("../../api.js"), a = require("../../utils.js"), o = getApp();

Page({
    data: {
        tab: 1,
        sort: 1,
        coupon_list: [],
        copy: !1,
        is_waimai: -1,
        carGoods: [],
        page_id: -1
    },
    onLoad: function(t) {
        o.pageOnLoad(this, t);
        var e = this;
        if ("undefined" == typeof my) {
            if (t.scene) {
                var s = decodeURIComponent(t.scene);
                s && (s = a.scene_decode(s)).mch_id && (t.mch_id = s.mch_id);
            }
        } else if (null !== o.query) {
            var i = o.query;
            o.query = null, t.mch_id = i.mch_id;
        }
        e.setData({
            tab: t.tab || 1,
            sort: t.sort || 1,
            mch_id: t.mch_id || !1,
            page_id: t.page_id || -1,
            cat_id: t.cat_id || ""
        }), e.data.mch_id || wx.showModal({
            title: "提示",
            content: "店铺不存在！店铺id为空"
        })
    },
    onReady: function() {
        o.pageOnReady(this);
    },
    onShow: function() {
        o.pageOnShow(this);
        var that = this
        wx.getSystemInfo({
          success: function (res) {
            if (res.model.indexOf("X") >= 0 && res.model.indexOf('iPhone') >= 0) {
              that.setData({
                isIphoneX: true
              })
            }
          }
        })
        setTimeout(() => {
            that.getShopData()
        }, 500);
    },
    onHide: function() {
        o.pageOnHide(this);
    },
    onUnload: function() {
        o.pageOnUnload(this);
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        //this.getGoodsList();
    },
    onShareAppMessage: function() {
        var t = this;
        return {
            title: t.data.shop ? t.data.shop.name : "商城首页"
        };
    },
    onShareTimeline: function () {
        var t = this;
        return {
          query: {mch_id:t.data.shop.id},
          imageUrl: t.data.shop ? t.data.shop.logo : "",
          title: t.data.shop ? t.data.shop.name : "商城首页"
        };
      },
    kfuStart: function() {
        this.setData({
            copy: !0
        });
    },
    kfuEnd: function() {
        this.setData({
            copy: !1
        });
    },
    copyinfo: function(t) {
        wx.setClipboardData({
            data: t.target.dataset.info,
            success: function(t) {
                wx.showToast({
                    title: "复制成功！",
                    icon: "success",
                    duration: 2e3,
                    mask: !0
                });
            }
        });
    },
    callPhone: function(t) {
        wx.makePhoneCall({
            phoneNumber: t.target.dataset.info
        });
    },

    getShopData: function () {
      var a = this, e = (a.data.current_page || 0) + 1, s = "shop_data_mch_id_" + a.data.mch_id, i = wx.getStorageSync(s);
      i && a.setData({
          shop: i.shop
      },function () {
          a.getCatNum(a.data.mch_id)
      }), wx.showNavigationBarLoading(), a.setData({
          loading: !0
      }),o.request({
        url: t.mch.shop_new,
        data: {
            mch_id: a.data.mch_id,
            tab: 1,
            page_id: a.data.page_id
        },
        success: function(t) {
          if (t.code == 0) {
            wx.setStorageSync(s, t.data);
            a.setData(t.data,function(){
                a.getCatNum(a.data.mch_id);
                if (t.data.info) {
                    wx.setNavigationBarTitle({
                        title: t.data.info
                    })
                }
            })
          }else {
            wx.showModal({
              title: "提示",
              content: "店铺不存在！"
            })
          }
        },
        complete: function() {
            wx.hideNavigationBarLoading(), a.setData({
                loading: !1
            });
        }
    });
    },
    getCatNum: function (mch_id) {
        var cat_info = wx.getStorageSync('mch_cat_goods_' + mch_id);
        var a = this;
        if (cat_info) {
            var r = a.data.cat_list;
            for (var i in r) {
                for(var h in r[i].goods_list) {
                    for (var u in cat_info) {
                        if (cat_info[u].goods_id == r[i].goods_list[h].id) {
                            r[i].goods_list[h].num = cat_info[u].num
                        }
                    }
                }
            }
            a.setData({
                carGoods: cat_info,
                cat_list: r
            },function (){
                a.carStatistics()
            })
        }else {
            a.setData({
                carGoods: [],
            },function (){
                a.carStatistics()
            })
        }
    },
  
    selectMenu: function(t) {
      var a = t.currentTarget.dataset, o = this.data.cat_list;
      for (var e = a.index, i = o.length, s = 0; s < i; s++){
        if (e == s) {
          o[s].cat_checked = 1
        }else {
          o[s].cat_checked = !1
        }
      }
      this.setData({
          toView: a.tag,
          cat_list: o,
      });
    },
    showDialogBtn: function(o) {
        var r = this, i = o.currentTarget.dataset;
        getApp().request({
            url: getApp().api.default.goods,
            data: {
                id: i.id
            },
            success: function(t) {
                0 == t.code && (r.setData({
                    currentGood: t.data,
                    goods_name: t.data.name,
                    attr_group_list: t.data.attr_group_list,
                    showModal: !0
                }), r.resetData(), r.updateData());
            }
        });
    },
    jia: function(t) {
        var a = this, o = t.currentTarget.dataset, r = a.data.cat_list;
        for (var i in r) for (var s in r[i].goods_list) if (parseInt(r[i].goods_list[s].id) === parseInt(o.id)) {
            var e = r[i].goods_list[s].num ? r[i].goods_list[s].num + 1 : 1;
            if (e > JSON.parse(r[i].goods_list[s].attr)[0].num) return void wx.showToast({
                title: "商品库存不足",
                image: "/images/icon-warning.png"
            });
            r[i].goods_list[s].num = e;
            var d = a.data.carGoods, c = 1, n = o.price ? o.price : r[i].goods_list[s].price;
            for (var u in d) {
                // if (parseInt(d[u].goods_id) === parseInt(o.id) && 1 === d[u].attr.length) {
                //     c = 0, d[u].num = e, d[u].goods_price = (d[u].num * d[u].price).toFixed(2);
                //     break;
                // }
                if (parseInt(d[u].goods_id) === parseInt(o.id)) {
                    c = 0, d[u].num = d[u].num + 1, d[u].goods_price = (d[u].num * d[u].price).toFixed(2);
                    break;
                }
            }
            if (1 === c || 0 === d.length) {
                var _ = JSON.parse(r[i].goods_list[s].attr);
                d.push({
                    goods_id: parseInt(r[i].goods_list[s].id),
                    attr: _[0].attr_list,
                    goods_name: r[i].goods_list[s].name,
                    cover_pic: r[i].goods_list[s].cover_pic,
                    goods_price: n,
                    num: 1,
                    price: n
                });
            }
        }
        a.setData({
            carGoods: d,
            cat_list: r
        }), a.carStatistics();
    },
    carStatistics: function() {
        var t = this, a = t.data.carGoods;
        wx.setStorageSync('mch_cat_goods_' + t.data.mch_id, a)
        var o = 0, r = 0;
        for (var i in a) o += a[i].num, r = parseFloat(r) + parseFloat(a[i].goods_price);
        var s = {
            total_num: o,
            total_price: r.toFixed(2)
        };
        0 === o && t.hideGoodsModel(), t.setData({
            total: s
        });
    },
    hideGoodsModel: function() {
        this.setData({
            goodsModel: !1
        });
    },
    jian: function(t) {
        var a = this, o = t.currentTarget.dataset, r = a.data.cat_list;
        for (var i in r) for (var s in r[i].goods_list) if (parseInt(r[i].goods_list[s].id) === parseInt(o.id)) {
            var e = r[i].goods_list[s].num > 0 ? r[i].goods_list[s].num - 1 : r[i].goods_list[s].num;
            r[i].goods_list[s].num = e;
            var d = a.data.carGoods;
            for (var c in d) {
                o.price ? o.price : r[i].goods_list[s].price;
                if (parseInt(d[c].goods_id) === parseInt(o.id) && 1 === d[c].attr.length) {
                    0, d[c].num = e, d[c].goods_price = (d[c].num * d[c].price).toFixed(2);
                    break;
                }
                if (parseInt(d[c].goods_id) === parseInt(o.id) ) {
                    0, d[c].num > 0 && (d[c].num = d[c].num - 1, d[c].goods_price = (d[c].num * d[c].price).toFixed(2));
                    break;
                }
            }
        }
        a.setData({
            carGoods: d,
            cat_list: r
        }), a.carStatistics();
    },
    resetData: function() {
        this.setData({
            checked_attr: [],
            check_num: 0,
            check_goods_price: 0,
            temporaryGood: {
                price: "0.00"
            }
        });
    },
    updateData: function() {
        var t = this, a = t.data.currentGood, o = t.data.carGoods, r = JSON.parse(a.attr), i = a.attr_group_list;
        for (var s in r) {
            var e = [];
            for (var d in r[s].attr_list) e.push([ r[s].attr_list[d].attr_id, a.id ]);
            for (var c in o) {
                var n = [];
                for (var u in o[c].attr) n.push([ o[c].attr[u].attr_id, o[c].goods_id ]);
                if (e.sort().join() === n.sort().join()) {
                    for (var _ in i) for (var g in i[_].attr_list) for (var l in e) {
                        if (parseInt(i[_].attr_list[g].attr_id) === parseInt(e[l])) {
                            i[_].attr_list[g].checked = !0;
                            break;
                        }
                        i[_].attr_list[g].checked = !1;
                    }
                    var h = {
                        price: o[c].price
                    };
                    return void t.setData({
                        attr_group_list: i,
                        check_num: o[c].num,
                        check_goods_price: o[c].goods_price,
                        checked_attr: e,
                        temporaryGood: h
                    });
                }
            }
        }
    },
    attrClick: function(t) {
        var a = this, o = t.target.dataset.groupId, r = t.target.dataset.id, i = a.data.attr_group_list, s = a.data.currentGood;
        for (var e in i) if (i[e].attr_group_id == o) for (var d in i[e].attr_list) i[e].attr_list[d].attr_id == r ? i[e].attr_list[d].checked = !0 : i[e].attr_list[d].checked = !1;
        var c = [];
        for (var e in i) for (var d in i[e].attr_list) !0 === i[e].attr_list[d].checked && c.push([ i[e].attr_list[d].attr_id, s.id ]);
        var n = JSON.parse(a.data.currentGood.attr), u = a.data.temporaryGood;
        for (var _ in n) {
            var g = [];
            for (var l in n[_].attr_list) g.push([ n[_].attr_list[l].attr_id, s.id ]);
            if (g.sort().join() === c.sort().join()) {
                if (0 === parseInt(n[_].num)) return void wx.showToast({
                    title: "商品库存不足，请选择其它规格或数量",
                    image: "/images/icon-warning.png"
                });
                u = parseFloat(n[_].price) ? {
                    price: n[_].price.toFixed(2)
                } : {
                    price: s.price.toFixed(2)
                };
            }
        }
        a.resetData(), a.checkUpdateData(c), a.setData({
            attr_group_list: i,
            temporaryGood: u,
            checked_attr: c
        });
    },
    checkUpdateData: function(t) {
        var a = this, o = a.data.carGoods;
        for (var r in o) {
            var i = [];
            for (var s in o[r].attr) i.push([ o[r].attr[s].attr_id, o[r].goods_id ]);
            i.sort().join() === t.sort().join() && a.setData({
                check_num: o[r].num,
                check_goods_price: o[r].goods_price
            });
        }
    },
    close_box: function(t) {
        this.setData({
            showModal: !1
        });
    },
    onConfirm: function(t) {
        var a = this, o = a.data.attr_group_list, r = a.data.checked_attr, i = a.data.currentGood;
        if (r.length === o.length) {
            var s = a.data.check_num ? a.data.check_num + 1 : 1, e = JSON.parse(i.attr);
            for (var d in e) {
                var c = [];
                for (var n in e[d].attr_list) if (c.push([ e[d].attr_list[n].attr_id, i.id ]), c.sort().join() === r.sort().join()) {
                    var u = e[d].price ? e[d].price : i.price, _ = e[d].attr_list;
                    if (s > e[d].num) return void wx.showToast({
                        title: "商品库存不足",
                        image: "/images/icon-warning.png"
                    });
                }
            }
            var g = a.data.carGoods, l = 1, h = (parseFloat(u) * s).toFixed(2);
            for (var p in g) {
                var f = [];
                for (var v in g[p].attr) f.push([ g[p].attr[v].attr_id, g[p].goods_id ]);
                if (f.sort().join() === r.sort().join()) {console.log('dddddddddd');
                    l = 0, g[p].num = g[p].num + 1, g[p].goods_price = (parseFloat(u) * g[p].num).toFixed(2);
                    break;
                }
            }
            1 !== l && 0 !== g.length || g.push({
                goods_id: i.id,
                attr: _,
                goods_name: i.name,
                cover_pic: i.cover_pic ? i.cover_pic : i.pic_list[0].pic_url,
                goods_price: u,
                num: 1,
                price: u
            }), a.setData({
                carGoods: g,
                check_goods_price: h,
                check_num: s
            }), a.carStatistics(), a.attrGoodStatistics();
        } else wx.showToast({
            title: "请选择规格",
            image: "/images/icon-warning.png"
        });
    },
    guigejian: function(t) {
        var a = this, o = a.data.checked_attr, r = a.data.carGoods, i = a.data.check_num ? --a.data.check_num : 1;
        a.data.currentGood;
        for (var s in r) {
            var e = [];
            for (var d in r[s].attr) e.push([ r[s].attr[d].attr_id, r[s].goods_id ]);
            if (e.sort().join() === o.sort().join()) return r[s].num > 0 && (r[s].num -= 1, 
            r[s].goods_price = (r[s].num * parseFloat(r[s].price)).toFixed(2)), a.setData({
                carGoods: r,
                check_goods_price: r[s].goods_price,
                check_num: i
            }), a.carStatistics(), void a.attrGoodStatistics();
        }
    },
    attrGoodStatistics: function() {
        var t = this, a = t.data.currentGood, o = t.data.carGoods, r = t.data.cat_list,  s = 0;
        for (var e in o) o[e].goods_id === a.id && (s += o[e].num);
        for (var e in r) for (var d in r[e].goods_list) parseInt(r[e].goods_list[d].id) === a.id && (r[e].goods_list[d].num = s);
        t.setData({
            cat_list: r,
        });
    },
    buynow: function(t) {
        var a = this, o = a.data.carGoods;
        a.data.goodsModel;
        a.setData({
            goodsModel: !1
        });
        for (var r = o.length, i = [], s = [], e = 0; e < r; e++) 0 != o[e].num && (s = {
            goods_id: o[e].goods_id,
            num: o[e].num,
            attr: o[e].attr
        }, i.push(s));
        var d = [];
        d.push({
            mch_id: a.data.mch_id,
            goods_list: i
        }), wx.navigateTo({
            url: "/pages/new-order-submit/new-order-submit?mch_list=" + JSON.stringify(d)
        }), a.clearCar();
    },
    clearCar: function(t) {
        var a = this;
        var  r = a.data.cat_list;
        for (var e in r) for (var d in r[e].goods_list) r[e].goods_list[d].num = 0;
        a.setData({
            goodsModel: !1,
            carGoods: [],
            total: {
                total_num: 0,
                total_price: 0
            },
            check_num: 0,
            cat_list: r,
            currentGood: [],
            checked_attr: [],
            check_goods_price: 0,
            temporaryGood: {}
        }), wx.removeStorageSync('mch_cat_goods_' + a.data.mch_id);
    },
    goodsModel: function(t) {
        var a = this, o = (a.data.carGoods, a.data.goodsModel);
        o ? a.setData({
            goodsModel: !1
        }) : a.setData({
            goodsModel: !0
        });
    },
    tianjia: function(t) {
        this.jia(t);
    },
    jianshao: function(t) {
        this.jian(t);
    },
    get_goods_info: function(t) {
        var a = this, o = a.data.carGoods, r = a.data.total, i = a.data.quick_hot_goods_lists, s = a.data.cat_lsit, e = {
            carGoods: o,
            total: r,
            quick_hot_goods_lists: i,
            check_num: a.data.check_num,
            quick_list: s
        };
       
        var d = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/pages/goods/goods?id=" + d
        });
    },
    jia_c: function(t) {
        console.log(t)
        t = t.detail;
        var goods = t.goods,a=this;
  
        var d = a.data.carGoods, c = 1, n = goods.price;
        for (var u in d) {
            if (parseInt(d[u].goods_id) === parseInt(goods.id) && 1 === d[u].attr.length) {
                c = 0, d[u].num = d[u].num + 1, d[u].goods_price = (d[u].num * d[u].price).toFixed(2);
                break;
            }
            if (d[u].price == parseFloat(goods.price)) {
                c = 0, d[u].num = d[u].num + 1, d[u].goods_price = (d[u].num * d[u].price).toFixed(2);
                break;
            }
        }
            
        if (1 === c || 0 === d.length) {
            var _ = goods.attr;
            d = d ? d : [];
            d.push({
                goods_id: parseInt(goods.id),
                attr: _[0].attr_list,
                goods_name: goods.name,
                cover_pic: goods.cover_pic,
                goods_price: n,
                num: 1,
                price: n
            });
        }
        
        a.setData({
            carGoods: d,
        }), a.carStatistics();
    },
    showDialogBtnC: function(o) {
        var r = this, i = o.detail.goods;
        getApp().request({
            url: getApp().api.default.goods,
            data: {
                id: i.id
            },
            success: function(t) {
                0 == t.code && (r.setData({
                    currentGood: t.data,
                    goods_name: t.data.name,
                    attr_group_list: t.data.attr_group_list,
                    showModal: !0
                }), r.resetData(), r.updateData());
            }
        });
    },
    showGetCoupon: function () {
        var that = this;
        that.setData({
            showCoupon: that.data.showCoupon ? 0 : 1
        })
    },
    navigatorClickBabber: function(t) {
        console.log('tttt', t)
        var e = t.type == 'navigatorClickBabber' ? t.detail.currentTarget.dataset.url : t.currentTarget.dataset.url;
        var ee = function(t) {
          var a = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
            e = /^[^\?]+\?([\w\W]+)$/.exec(t),
            i = {};
          if (e && e[1])
            for (var o, r = e[1]; null != (o = a.exec(r));) i[o[1]] = o[2];
          return i;
        }(e)
        var open_type = t.type == 'navigatorClickBabber' ? t.detail.currentTarget.dataset.open_type : t.currentTarget.dataset.open_type;
        if (open_type == 'wxapp') {
          var extraData = function(t) {
            var a = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
              e = /^[^\?]+\?([\w\W]+)$/.exec(t),
              i = {};
            if (e && e[1])
              for (var o, r = e[1]; null != (o = a.exec(r));) i[o[1]] = o[2];
            return i;
          }(decodeURIComponent(ee.path))
          wx.navigateToMiniProgram({
            appId: ee.appId,
            path:decodeURIComponent(ee.path),
            extraData:extraData,
            fail: function () {
              
            }
          })
          return ;
        }
        // babber 链接
      var babber_url = [{
        "pagePath": "pages/index/index"
      },
      {
        "pagePath": "pages/cat/cat"
      },
      {
        "pagePath": "pages/cart/cart"
      },
      {
        "pagePath": "pages/user/user"
      },
      {
        "pagePath": "pages/posts/index"
      }
    ];
    var is_babber = 0;
    for (var i = 0; i < babber_url.length; i++) {
      if (e.indexOf(babber_url[i].pagePath) != -1) {
        if (ee.cat_id != '') {
          getApp().globalData.tab_cat_id = ee.cat_id;
          is_babber = 1;
        } else {
          is_babber = 1;
        }

      }
    }
    if (is_babber) {
      wx.switchTab({
        url: e
      })
    } else {
      wx.navigateTo({
        url: e　　 // 页面 A
      })
    }

    return false;




    }
});